package com.auth.manager.service.sys.impl;

import com.auth.manager.common.ex.BizException;
import com.auth.manager.mapper.sys.RoleMenuRelationMapper;
import com.auth.manager.pojo.sys.RoleMenuRelation;
import com.auth.manager.service.sys.RoleMenuRelationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @version 1.0.0
 * @Date: 2022/4/28 20:53
 * @Author ZhuYouBin
 * @Description 角色-权限关系
 */
@Service
public class RoleMenuRelationServiceImpl extends ServiceImpl<RoleMenuRelationMapper, RoleMenuRelation> implements RoleMenuRelationService {

    @Transactional
    @Override
    public void assignMenuToRole(List<RoleMenuRelation> list) {
        if (CollectionUtils.isEmpty(list)) {
            throw new BizException("分配权限失败");
        }
        // 批量删除角色-权限关系数据
        Integer rid = list.get(0).getRid();
        QueryWrapper<RoleMenuRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("rid", rid);
        try {
            this.remove(wrapper);
            // 批量新增角色-权限关系数据
            this.saveBatch(list);
        } catch (Exception e) {
            throw new BizException("分配权限失败");
        }
    }

}
